package quick.portal.fragment.workorder;

import java.util.List;

import com.alibaba.fastjson.JSONArray;

import quick.common.AppContext;
import quick.portal.R;
import quick.portal.activity.WebviewActivity;
import quick.portal.data.WorkOrder;
import quick.portal.fragment.DataListFragment;
import quick.portal.service.BaseService;
import quick.view.quickadapter.BaseAdapterHelper;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.TextView;

public class WorkOrderFragment extends DataListFragment<WorkOrder> {
	
	public WorkOrderFragment() {
		this.queryAction = "WorkOrderList";
    	this.itemViewLayoutId = R.layout.data_list_item_workorder;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		PopupMenu menu = this.GetOriginalPopupMenu();
		menu.getMenu().add(R.id.menu_group_data,R.id.menu_add_workorder,menu.getMenu().size(),"新建工单");
		menu.setOnMenuItemClickListener(this);
	}


	@Override
	public boolean onMenuItemClick(MenuItem item) {
		super.onMenuItemClick(item);
		
	    if (item.getItemId() == R.id.menu_add_workorder) {
	    	String url = BaseService.getPortalUrl();
	    	url = url + "/Systems/WorkOrderInfo/Add/";
	    	
	    	String token = AppContext.getInstance().getToken();
	    	url = url + "?" + AppContext.Login_Token_Key + "=" + token;

	        Intent intent = new Intent(context,WebviewActivity.class);
	        intent.putExtra("Url", url);
	        startActivity(intent);
	        
	    	return true;
	    }
	    
	    return false;
	}
	
	@Override
	protected void adapterConvert(BaseAdapterHelper helper, WorkOrder order) {
		helper.setText(R.id.name, order.getName())
        .setTag(R.id.name, order)
        .setText(R.id.formState, order.getFormState())
		.setText(R.id.createTime, order.getCreateTime());
		
	}

	@Override
	protected void showDetail(View view) {
		TextView tvName = (TextView)view.findViewById(R.id.name);
		WorkOrder order = (WorkOrder)tvName.getTag();
		
    	String url = BaseService.getPortalUrl();
    	if(order.getFormState() == "草稿")
    	{
    		url = url + "/Systems/WorkOrderInfo/Edit/" + order.getId() + "/";
    	}
    	else
    	{
    		url = url + "/Systems/WorkOrderInfo/Detail/" + order.getId() + "/";
    	}
    	
    	String token = AppContext.getInstance().getToken();
    	url = url + "?" + AppContext.Login_Token_Key + "=" + token;

        Intent intent = new Intent(context,WebviewActivity.class);
        intent.putExtra("Url", url);
        startActivity(intent);

        
	}

	@Override
	protected List<WorkOrder> parseArray(String body) {
		List<WorkOrder> list = JSONArray.parseArray(body, WorkOrder.class);
		
		return list;
	}

}
